<script setup lang="ts">
	import { http } from '@/utils/http';
	import { onLoad } from '@dcloudio/uni-app';
	import { computed, ref } from 'vue';
	import type {
		SkuPopupEvent,
		SkuPopupInstance,
		SkuPopupLocaldata,
	} from '@/components/vk-data-goods-sku-popup/vk-data-goods-sku-popup'
	// const goods = ref<GoodsResult>();
	// const getGoodsByIdData = async  () => {
	// 	const res = await http({
	// 		url: "/goods",
	// 		method: "GET",
	// 		data: {
	// 			id: ids.value
	// 		}
	// 	})
	// 	console.log(res)
	// }

	const isShowSku = ref(false);
	const localdata = ref([]);
	const { safeAreaInserts } = uni.getSystemInfoSync();
	const ids = ref("");
	const obj = ref({});
	onLoad((option) => {
		console.log(option)
		ids.value = option.id
		getdata(ids.value)
		getdata1()
	})
	const ii = ref(0);
	const objlist = ref([]);
	const getdata1 = async () => {
		const res = await http({
			url: "/member/address",
			method: "get"
		})
		console.log(res)
		objlist.value = res.result
	}
	const getdata = async (e) => {
		console.log(e)
		const res = await http({
			url: `/goods?id=${e}`,
			method: "GET"
		});
		console.log(res)
		obj.value = res.result
	}
	const zhuandong = (e) => {
		console.log(e)
		ii.value = e.detail.current;
	}
	const popup = ref("popup")
	const popup1 = ref("popup1")

	const fw = () => {
		console.log(1)
		popup.value.open();
	}
	const dz = () => {
		popup1.value.open();

	}
	const close = () => {
		popup.value.close();

	}
	const close1 = () => {
		popup1.value.close();
	}

	enum SkuMode {
		Both = 1,
		Cart = 2,
		Buy = 3
	}
	const skuPopupRef = ref<SkuPopupInstance>()
	const searchtext = computed(() => {

		return skuPopupRef.value?.selectArr?.join(' ').trim() || '请选择商品规格'
	})
	const mode = ref<SkuMode>(SkuMode.Both)
	const xzz = ref("");
	const address = ref("");
	const xz = (index) => {
		xzz.value = index
		objlist.value.forEach((el, index) => {
			if (index == xzz.value) {
				console.log(el)
				address.value = el.fullLocation + el.address
			}
		})
	}
	const gg = () => {
		isShowSku.value = true

		localdata.value = {
			_id: obj.value.id,
			name: obj.value.name,
			goods_thumb: obj.value.mainPictures[0],
			spec_list: obj.value.specs.map((v) => ({ name: v.name, list: v.values })),
			sku_list: obj.value.skus.map((v) => ({
				_id: v.id,
				goods_id: obj.value.id,
				goods_name: obj.value.name,
				image: v.picture,
				price: v.price * 100, // 注意：需要乘以 100
				stock: v.inventory,
				sku_name_arr: v.specs.map((vv) => vv.valueName),
			})),
		}
	}
	const addcart=async(e)=>{
		console.log(e)
		const res=await http({
			url:"/member/cart",
			method:"POST",
			data:{
				skuId:e._id,
				count:e.buy_num
			}
		})
		console.log(res)
		uni.showToast({
			title:"新增成功"
		})
		isShowSku.value=false
	}
</script>
<template>
	<vk-data-goods-sku-popup ref="skuPopupRef" :mode="mode" v-model="isShowSku" @add-cart="addcart" :localdata="localdata" />
	<scroll-view scroll-y class="viewport">
		<view class="goods">
			<view class="preview">
				<swiper :indicator-dots="true" :autoplay="true" @change="zhuandong" :interval="3000" :duration="1000">
					<swiper-item v-for="(item,index) in obj.mainPictures">
						<image mode="aspectFill" :src="item" />
					</swiper-item>
				</swiper>
				<view class="indicator">
					<text class="current">{{ii+1}}</text>
					<text class="split">/</text>
					<text class="total">{{obj.mainPictures.length}}</text>

				</view>
			</view>
			<view class="meta">
				<view class="price">
					<text class="symbol">￥</text>
					<text class="number">{{obj.price}}</text>
				</view>
				<view class="name ellipsis">
					{{obj.name}}

				</view>
				<view class="desc">
					{{obj.desc}}

				</view>
			</view>
			<view class="action">
				<view class="item arrow" @click="gg">
					<text class="label">选择</text>
					<text class="text ellipsis">{{searchtext}}</text>

				</view>
				<view class="item arrow" @click="dz">
					<text class="label">送至</text>
					<text class="text ellipsis">{{address}}</text>

				</view>

				<view class="item arrow" @click="fw">
					<text class="label">服务</text>
					<text class="text ellipsis">无忧退 快速退款 免费包邮</text>

				</view>

			</view>
		</view>
		<uni-popup ref="popup1" type="bottom" background-color="#fff">
			<view class="address-panel">
				<text class="close icon-close" @click="close1"></text>
				<view class="title">配送至</view>
				<view class="content">
					<view class="item" @click="xz(index)" v-for="(item,index) in objlist">
						<view class="user">
							{{item.receiver}} {{item.contact}}
						</view>
						<view class="address">
							{{item.fullLocation}}
						</view>
						<text class="icon icon-checked" v-if="xzz == index"></text>
					</view>



				</view>
				<view class="footer">
					<view class="button primary">
						<navigator hover-class="none" url="/pagesMember/address-form/address-form">新建地址</navigator>
					</view>
					<view class="button primary" v-if="false">

					</view>

				</view>
			</view>

		</uni-popup>
		<uni-popup ref="popup" type="bottom" background-color="#fff">
			<view class="service-panel">
				<text class="close icon-close" @click="close"></text>
				<view class="title">
					服务说明
				</view>
				<view class="content">
					<view class="item">
						<view class="dt">无忧退货
						</view>
						<view class="dd">
							自收到商品之日起30天内，可在线申请无忧退货服务（食品等特殊商品除外）

						</view>
					</view>
					<view class="item">
						<view class="dt">无忧退货
						</view>
						<view class="dd">
							自收到商品之日起30天内，可在线申请无忧退货服务（食品等特殊商品除外）

						</view>
					</view>
					<view class="item">
						<view class="dt">无忧退货
						</view>
						<view class="dd">
							自收到商品之日起30天内，可在线申请无忧退货服务（食品等特殊商品除外）

						</view>
					</view>

				</view>
			</view>
		</uni-popup>
		<view class="detail panel">
			<view class="title">
				<text>详情</text>
			</view>
			<view class="content">
				<view class="properties">
					<view class="item" v-for="(item,index) in obj.details.properties">
						<text class="label">{{item.name}}</text>
						<text class="value">{{item.value}}</text>

					</view>

				</view>
				<image v-for="(item,index) in obj.details.pictures" mode="widthFix" :src="item">
				</image>

			</view>
		</view>
		<view class="similar panel">
			<view class="title">
				<text>同类推荐</text>
			</view>
			<view class="content">
				<navigator v-for="item in obj.hotByDay" :key="item" class="goods" hover-class="none"
					:url="`/pages/goods/goods?id=${item.id}`">
					<image class="image" mode="aspectFill" :src="item.picture"></image>
					<view class="name ellipsis">
						{{item.name}}

					</view>
					<view class="price">
						<text class="symbol">￥</text>
						<text class="number">{{item.price}}</text>

					</view>
				</navigator>

			</view>
		</view>
	</scroll-view>
	<view class="toolbar" :style="{paddingBottom:safeAreaInserts?.bottom + 'px'}">
		<view class="icons">
			<button class="icons-button"><text class="icon-heart"></text>收藏</button>
			<button class="icons-button" open-type="contact"><text class="icon-handset"></text>客服</button>
			<navigator class="icons-button" url="/pages/cart/cart" open-type="switchTab"> <text
					class="icon-cart"></text>购物车 </navigator>
		</view>
		<view class="buttons">
			<view class="addcart">加入购物车

			</view>
			<view class="buynow">
				立即购买

			</view>

		</view>

	</view>
</template>
<style lang="scss">
	.address-panel {
		padding: 0 30rpx;
		border-radius: 10rpx 10rpx 0 0;
		position: relative;
		background-color: #fff;
	}

	.title {
		line-height: 1;
		padding: 40rpx 0;
		text-align: center;
		font-size: 32rpx;
		font-weight: normal;
		border-bottom: 1rpx solid #ddd;
		color: #444;
	}

	.close {
		position: absolute;
		right: 24rpx;
		top: 24rpx;
	}

	.content {
		min-height: 300rpx;
		max-height: 540rpx;
		overflow: auto;
		padding: 20rpx;

		.item {
			padding: 30rpx 50rpx 30rpx 60rpx;
			background-size: 40rpx;
			background-repeat: no-repeat;
			background-position: 0 center;
			background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
			position: relative;
		}

		.icon {
			color: #999;
			font-size: 40rpx;
			transform: translateY(-50%);
			position: absolute;
			top: 50%;
			right: 0;
		}

		.icon-checked {
			color: #27ba9b;
		}

		.icon-ring {
			color: #444;
		}

		.user {
			font-size: 28rpx;
			color: #444;
			font-weight: 500;
		}

		.address {
			font-size: 26rpx;
			color: #666;
		}
	}

	.footer {
		display: flex;
		justify-content: space-between;
		padding: 20rpx 0 40rpx;
		font-size: 28rpx;
		color: #444;

		.button {
			flex: 1;
			height: 72rpx;
			text-align: center;
			line-height: 72rpx;
			margin: 0 20rpx;
			color: #fff;
			border-radius: 72rpx;
		}

		.primary {
			color: #fff;
			background-color: #27ba9b;
		}

		.secondary {
			background-color: #ffa868;
		}
	}
</style>
<style lang="scss">
	.service-panel {
		padding: 0 30rpx;
		border-radius: 10rpx 10rpx 0 0;
		position: relative;
		background-color: #fff;
	}

	.title {
		line-height: 1;
		padding: 40rpx 0;
		text-align: center;
		font-size: 32rpx;
		font-weight: normal;
		border-bottom: 1rpx solid #ddd;
		color: #444;
	}

	.close {
		position: absolute;
		right: 24rpx;
		top: 24rpx;
	}

	.content {
		padding: 20rpx 20rpx 100rpx 20rpx;

		.item {
			margin-top: 20rpx;
		}

		.dt {
			margin-bottom: 10rpx;
			font-size: 28rpx;
			color: #333;
			font-weight: 500;
			position: relative;

			&::before {
				content: '';
				width: 10rpx;
				height: 10rpx;
				border-radius: 50%;
				background-color: #eaeaea;
				transform: translateY(-50%);
				position: absolute;
				top: 50%;
				left: -20rpx;
			}
		}

		.dd {
			line-height: 1.6;
			font-size: 26rpx;
			color: #999;
		}
	}
</style>
<style lang="scss">
	page {
		height: 100%;
		overflow: hidden;
		display: flex;
		flex-direction: column;
	}

	.viewport {
		background-color: #f4f4f4;
	}

	.panel {
		margin-top: 20rpx;
		background-color: #fff;

		.title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 90rpx;
			line-height: 1;
			padding: 30rpx 60rpx 30rpx 6rpx;
			position: relative;

			text {
				padding-left: 10rpx;
				font-size: 28rpx;
				color: #333;
				font-weight: 600;
				border-left: 4rpx solid #27ba9b;
			}

			navigator {
				font-size: 24rpx;
				color: #666;
			}
		}
	}

	.arrow {
		&::after {
			position: absolute;
			top: 50%;
			right: 30rpx;
			content: '\e6c2';
			color: #ccc;
			font-family: 'erabbit' !important;
			font-size: 32rpx;
			transform: translateY(-50%);
		}
	}

	/* 商品信息 */
	.goods {
		background-color: #fff;

		.preview {
			height: 750rpx;
			position: relative;

			.indicator {
				height: 40rpx;
				padding: 0 24rpx;
				line-height: 40rpx;
				border-radius: 30rpx;
				color: #fff;
				font-family: Arial, Helvetica, sans-serif;
				background-color: rgba(0, 0, 0, 0.3);
				position: absolute;
				bottom: 30rpx;
				right: 30rpx;

				.current {
					font-size: 26rpx;
				}

				.split {
					font-size: 24rpx;
					margin: 0 1rpx 0 2rpx;
				}

				.total {
					font-size: 24rpx;
				}
			}
		}

		.meta {
			position: relative;
			border-bottom: 1rpx solid #eaeaea;

			.price {
				height: 130rpx;
				padding: 25rpx 30rpx 0;
				color: #fff;
				font-size: 34rpx;
				box-sizing: border-box;
				background-color: #35c8a9;
			}

			.number {
				font-size: 56rpx;
			}

			.brand {
				width: 160rpx;
				height: 80rpx;
				overflow: hidden;
				position: absolute;
				top: 26rpx;
				right: 30rpx;
			}

			.name {
				max-height: 88rpx;
				line-height: 1.4;
				margin: 20rpx;
				font-size: 32rpx;
				color: #333;
			}

			.desc {
				line-height: 1;
				padding: 0 20rpx 30rpx;
				font-size: 24rpx;
				color: #cf4444;
			}
		}

		.action {
			padding-left: 20rpx;

			.item {
				height: 90rpx;
				padding-right: 60rpx;
				border-bottom: 1rpx solid #eaeaea;
				font-size: 26rpx;
				color: #333;
				position: relative;
				display: flex;
				align-items: center;

				&:last-child {
					border-bottom: 0 none;
				}
			}

			.label {
				width: 60rpx;
				color: #898b94;
				margin: 0 16rpx 0 10rpx;
			}

			.text {
				flex: 1;
				-webkit-line-clamp: 1;
			}
		}
	}

	/* 商品详情 */
	.detail {
		padding-left: 20rpx;

		.content {
			margin-left: -20rpx;
		}

		.properties {
			padding: 0 20rpx;
			margin-bottom: 30rpx;

			.item {
				display: flex;
				line-height: 2;
				padding: 10rpx;
				font-size: 26rpx;
				color: #333;
				border-bottom: 1rpx dashed #ccc;
			}

			.label {
				width: 200rpx;
			}

			.value {
				flex: 1;
			}
		}
	}

	/* 同类推荐 */
	.similar {
		padding-left: 20rpx;

		.content {
			padding: 0 20rpx 20rpx;
			margin-left: -20rpx;
			background-color: #f4f4f4;
			overflow: auto;

			navigator {
				width: 345rpx;
				padding: 24rpx 20rpx 20rpx;
				margin: 20rpx 20rpx 0 0;
				border-radius: 10rpx;
				background-color: #fff;
				float: left;
			}

			.image {
				height: 260rpx;
			}

			.name {
				height: 80rpx;
				margin: 10rpx 0;
				font-size: 26rpx;
				color: #262626;
			}

			.price {
				line-height: 1;
				font-size: 20rpx;
				color: #cf4444;
			}

			.number {
				font-size: 26rpx;
				margin-left: 2rpx;
			}
		}

		navigator {
			&:nth-child(even) {
				margin-right: 0;
			}
		}
	}

	/* 底部工具栏 */
	.toolbar {
		background-color: #fff;
		height: 100rpx;
		padding: 0 20rpx;
		border-top: 1rpx solid #eaeaea;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: content-box;

		.buttons {
			display: flex;

			&>view {
				width: 220rpx;
				text-align: center;
				line-height: 72rpx;
				font-size: 26rpx;
				color: #fff;
				border-radius: 72rpx;
			}

			.addcart {
				background-color: #ffa868;
			}

			.buynow {
				background-color: #27ba9b;
				margin-left: 20rpx;
			}
		}

		.icons {
			padding-right: 10rpx;
			display: flex;
			align-items: center;
			flex: 1;

			.icons-button {
				flex: 1;
				text-align: center;
				line-height: 1.4;
				padding: 0;
				margin: 0;
				border-radius: 0;
				font-size: 20rpx;
				color: #333;
				background-color: #fff;
			}

			text {
				display: block;
				font-size: 34rpx;
			}
		}
	}
</style>